package com.hooker.settings;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.hooker.common.StringUtil;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Setting {
    public static final String H_APPS = "h.apps";
    public static final String H_PLUGINS = "h.plugins";
    /**
     * hook的关联项
     */
    public static final String H_INJECTS = "h.injects";
    public static final String SPLITSTR = "___";

    private final static String Authorities = "setting.api";
    private final static String AuthoritiesBase = "content://" + Authorities + "/";


    public static String get(Context context, String name) {
        Cursor query = context.getContentResolver().query(Uri.parse(AuthoritiesBase + "gl?" + name + "=true"), new String[0], "", new String[0], "");

        while (query != null && query.moveToNext()) {
            return query.getString(1);
        }

        return null;
    }

    public static void put(Context context, String name, String value) {
        context.getContentResolver().insert(Uri.parse(AuthoritiesBase + "gl?" + name + "=" + value), new ContentValues());
    }

    public static void hook(Context context, String app) {
        String string = get(context, H_APPS);

        Set<String> hookApps = getHookApps(context);

        if (hookApps.contains(app)) {
            return;
        }

        if (string == null || "".equals(string)) {
            put(context, H_APPS, app);
        } else {
            put(context, H_APPS, string + SPLITSTR + app);
        }
    }

    public static void unHook(Context context, String app) {
        Set<String> hookApps = getHookApps(context);

        if (hookApps.contains(app)) {
            hookApps.remove(app);

            StringBuilder builder = new StringBuilder();
            StringUtil.join(builder, hookApps, SPLITSTR);

            put(context, H_APPS, builder.toString());

            return;
        }
    }

    public static void addPlugin(Context context, String plugin) {
        put(context, H_PLUGINS, plugin);
    }

    public static Set<String> getHookApps(Context c) {
        String string = get(c, Setting.H_APPS);

        if (string != null) {
            String[] apps = string.split(SPLITSTR);

            return new HashSet<String>(Arrays.asList(apps));
        }

        return new HashSet<>();
    }

    public static Set<String> getHookPlugin(Context c) {
        String string = get(c, H_PLUGINS);

        return new HashSet<String>(Arrays.asList(string));
    }

    public static boolean isDebug(Context context) {
        try {
            String string = get(context, "h.debug");

            return "true".equals(string);
        } catch (Exception e) {
            return true;
        }
    }

    public static void setDebug(Context context, boolean debug) {
        put(context, "h.debug", String.valueOf(debug));
    }

    public static void clear(Context context) {
        put(context, "h.apps", null);
        put(context, "h.plugins", null);
        put(context, "h.debug", null);
        put(context, H_INJECTS, null);
    }

    public static void setStopApp(Context context, String name) {
        put(context, "h.stop", name);
    }

    public static String getStopApp(Context context) {
        return get(context, "h.stop");
    }

    public static void setPluginClass(Context context, String name) {
        put(context, "h.class", name);
    }

    public static String getPluginClass(Context context) {
        return get(context, "h.class");
    }

    public static void inject(Context context, String value) {
        put(context, H_INJECTS, value);
    }

    public static String getInjectValue(Context context) {
        return get(context, H_INJECTS);
    }
}
